<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加按钮 -->
      <el-button class="btnbottom" type="primary" @click="addType">添加分类</el-button>
      <!-- 表单内容区域 -->
      <tree-table
        :data="catelistData"
        :columns="columns"
        border
        show-index
        index-text="#"
        show-row-hover
        :selection-type="false"
        :expand-type="false">
        <!-- 作用域插槽的模板 -->
        <template slot="isok" slot-scope="scope">
          <!-- {{scope.row.cat_deleted}} -->
          <i
            style="color:#67C23A"
            v-if="scope.row.cat_deleted"
            class="el-icon-success"
          ></i>
          <i style="color:red" class="el-icon-error" v-else></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <!-- {{scope.row.cat_level}} -->
          <el-tag v-if="scope.row.cat_level == 0" size="mini">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level == 1"
            type="success"
            size="mini"
            >二级</el-tag
          >
          <el-tag v-else type="warning" size="mini">三级</el-tag>
        </template>
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" size="mini" @click="optCate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteDiolog(scope.row)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类弹出层窗口 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible" width="50%" @close="resetAddCate()">
      <el-form :model="addCateObj"  :rules="rules"  ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateObj.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
            <!-- options 指定数据源 -->
            <!-- props 指定配置对象 -->
         <el-cascader
         v-model="selectedKeys"
         :options="parentCateList"
         @change="handleChange"
         :props="{value: cascaderProps.value , label: cascaderProps.label, children: cascaderProps.children ,expandTrigger :'hover'}"
         clearable
         change-on-select
         size="medium"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAdd(addCateObj)">确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 编辑窗口弹出层窗口 -->
    <el-dialog title="编辑分类"  :visible.sync="optDialogVisible" width="50%">
         <el-form :model="optCateData"  :rules="rules"  ref="optCateData" label-width="100px">
          <el-form-item label="分类ID">
          <el-input v-model="optCateData.cat_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="optCateData.cat_name"></el-input>
        </el-form-item>
         </el-form>
         <span slot="footer" class="dialog-footer">
        <el-button @click="optDialogVisible = false">取消</el-button>
        <el-button @click="submitOptCate()" type="primary" >确定</el-button>
      </span>
    </el-dialog>
    <!-- 删除窗口弹出层 -->
    <el-dialog title="删除此分类" :visible.sync="deleteDialog" width="30%">
      <span>
          <i class="el-icon-delete-solid red"></i>
          确定删除该分类？此操作将永久删除</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialog = false">取消</el-button>
        <el-button @click="deleteCate()" type="primary" >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      //   商品数据列表
      catelistData: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      //   为表指定数据列的配置
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' }, // 作用域插槽
        { label: '排序', type: 'template', template: 'sort' },
        { label: '操作', type: 'template', template: 'operation' }
      ],
      //   添加分类窗口
      dialogFormVisible: false,
      addCateObj: {
        cat_pid: 0,
        cat_level: 0,
        cat_name: ''
      },
      //   表单验证规则
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      //   添加窗口里面的父级分类列表
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      //   被选中的父级分类id数组
      selectedKeys: [],
      //   编辑窗口
      optDialogVisible: false,
      optCateData: {},
      //   删除分类
      deleteDialog: false,
      deleteCateData: {}
    }
  },
  created () {
    this.getcatelistData()
  },
  methods: {
    // 1\获取商品分类数据列表
    async getcatelistData () {
      // get请求的参数要用params接收
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      //   console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类数据列表失败')
      }
      this.catelistData = res.data.result
      this.total = res.data.total
      // console.log(this.catelistData)
    },
    // 2\分页功能
    handleSizeChange (newSize) {
      //   console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getcatelistData()
    },
    handleCurrentChange (newPage) {
      //   console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getcatelistData()
    },
    // 3\添加分类按钮，点击弹出窗口,调用接口获取商品分类数据列表
    async addType () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类数据失败')
      this.parentCateList = res.data
      //   console.log(res.data)
      this.dialogFormVisible = true
    },
    // 选择父级分类后发生变化的方法
    handleChange () {
      if (this.selectedKeys.length > 0) {
        // 父级分类的id
        this.addCateObj.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        //    为当前分类的等级赋值
        this.addCateObj.cat_level = this.selectedKeys.length
      } else {
        this.addCateObj.cat_pid = 0
        this.addCateObj.cat_level = 0
      }
    },
    // 点击确定按钮，提交表单内容，调用接口，关闭窗口
    submitAdd (data) {
      this.$refs.addCateRef.validate(async (valid) => {
        if (valid) {
          console.log(this.addCateObj)
          const { data: res } = await this.$http.post('categories', this.addCateObj)
          if (res.meta.status !== 201) return this.$message.error('添加失败')
          this.getcatelistData()
          this.dialogFormVisible = false
          this.$message.success('添加新的分类成功')
        } else {
          return false
        }
      })
    },
    // 重置表单
    resetAddCate () {
      this.$refs.addCateRef.resetFields()
    },
    // 4、编辑分类
    async optCate (catedata) {
      const { data: res } = await this.$http.get('categories/' + catedata.cat_id)
      this.optCateData = res.data
      this.optDialogVisible = true
    },
    // 点击确定按钮，提交修改的分类，调用后台接口
    async submitOptCate () {
      const { data: res } = await this.$http.put('categories/' + this.optCateData.cat_id, { cat_name: this.optCateData.cat_name })
      //   console.log(res)
      if (res.meta.status !== 200) return this.$message.error('更新失败')
      this.getcatelistData()
      this.optDialogVisible = false
      this.$message.success('修改成功')
    },
    // 5、删除该分类
    async deleteDiolog (catedata) {
      this.deleteDialog = true
      console.log('要删除的分类id：' + catedata.cat_id)
      const { data: res } = await this.$http.get('categories/' + catedata.cat_id)
      this.deleteCateData = res.data
      console.log('要被删除分类的数据：' + res)
    },
    // 点击确定按钮，调用接口删除分类
    async deleteCate () {
      const { data: res } = await this.$http.delete('categories/' + this.deleteCateData.cat_id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.getcatelistData()
      this.deleteDialog = false
      this.$message.success('删除成功')
    }
  }
}
</script>

<style lang="less" scoped>
.btnbottom,
.el-breadcrumb {
  margin-bottom: 15px;
}
.red {
  color: red;
}
.el-cascader {
    width: 100%;
}
</style>
